package com.zhengxiang.onlinexamination.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhengxiang.onlinexamination.base.BaseEntity;
import com.zhengxiang.onlinexamination.dao.ExamPaperMapper;
import com.zhengxiang.onlinexamination.dao.QuMapper;
import com.zhengxiang.onlinexamination.entity.ExamPaper;
import com.zhengxiang.onlinexamination.entity.PaperQu;
import com.zhengxiang.onlinexamination.entity.vo.ExamPaperVo;
import com.zhengxiang.onlinexamination.entity.vo.QuestionFormVo;
import com.zhengxiang.onlinexamination.service.PaperQuService;
import com.zhengxiang.onlinexamination.service.ExamPaperService;
import com.zhengxiang.onlinexamination.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 试卷(ExamPaper)表服务实现类
 *
 * @author
 * @since 2023-03-13 11:50:36
 */
@Transactional(rollbackFor = Exception.class)
@Service("ExamPaperService")
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {
    @Resource
    private ExamPaperMapper ExamPaperMapper;


    @Autowired
    private PaperQuService paperQuService;

    @Autowired
    private QuMapper quMapper;


    @Override
    public IPage<ExamPaper> queryAll(ExamPaperVo examPaperVo) {
        QueryWrapper<ExamPaper> q = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(examPaperVo.getPaperName())) {
            q.lambda().like(ExamPaper::getPaperName, examPaperVo.getPaperName());
        }
        if (Objects.nonNull(examPaperVo.getStartDate()) && Objects.nonNull(examPaperVo.getEndDate())) {
            q.ge("DATE_FORMAT(create_time,'%Y-%m-%d')", examPaperVo.getStartDate());
            q.le("DATE_FORMAT(create_time,'%Y-%m-%d')", examPaperVo.getEndDate());
        }
        q.lambda().orderByDesc(ExamPaper::getCreateTime, ExamPaper::getUpdateTime);
        Page<ExamPaper> ExamPaperPage = ExamPaperMapper.selectPage(new Page<ExamPaper>(examPaperVo.getCurrent(), examPaperVo.getSize()), q);
        return ExamPaperPage;
    }


    @Override
    public void savePaper(ExamPaperVo ppt) {
        ppt.setPaperNo(IdUtil.createSnowflake(1, 1).nextIdStr());
        ppt.setQuestionNum(ppt.getQuIds().size());
        ExamPaperMapper.insert(ppt);
        //勾选的题库
        List<QuestionFormVo> qus = ppt.getQus();
        //从勾选的题库中随机选出对于的题目
        List<Integer> quIds = new ArrayList<>();
        qus.forEach(s -> {
            //单选题
            if (s.getSingleQu() > 0) {
                quIds.addAll(quMapper.randQu(s.getQuBankId(), 1, s.getSingleQu()));
            }
            // 多选题
            if (s.getMultipleQu() > 0) {
                quIds.addAll(quMapper.randQu(s.getQuBankId(), 2, s.getMultipleQu()));
            }
            //判断题
            if (s.getJudgmentQu() > 0) {
                quIds.addAll(quMapper.randQu(s.getQuBankId(), 3, s.getJudgmentQu()));
            }
            //简答题
            if (s.getShortQu() > 0) {
                quIds.addAll(quMapper.randQu(s.getQuBankId(), 4, s.getShortQu()));
            }
        });
        List<PaperQu> paperQus = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(quIds)) {
            quIds.forEach(quId -> {
                PaperQu paperQu = new PaperQu();
                paperQu.setExamPaperId(ppt.getExamPaperId());
                paperQu.setQuId(quId);
                paperQus.add(paperQu);
            });
        }
        paperQuService.saveBatch(paperQus);

    }
}
